var month = new Vue({
	el: '#month-container',
	data: {
		years: [],
		months: [],
		dates: [{
			"helpMonth": 9,
			"helpYear": 2016
		}],
		selected: {
			"year": '',
			"month": ''
		},
		je: '',
		url: {
			'modifyMoney': 'http://help.ngrok.cc/studentmanagement/mgr/je/add.do',
			'getMoney': 'http://help.ngrok.cc/studentmanagement/mgr/je/get.do',
			'addMonth': 'http://help.ngrok.cc/studentmanagement/usr/date/add.do',
			'delMonth': 'http://help.ngrok.cc/studentmanagement/usr/date/remove.do',
			'getMonths': 'http://help.ngrok.cc/studentmanagement/usr/date/getAll.do'
		}
	},
	methods: {
		/*初始化列表*/
		init: function() {
			var _self = this;
			// 获取当年的年月,设置select
			var myDate = new Date();
			var year = myDate.getFullYear();
			for (var i = -5; i < 5; i++) {
				_self.years.push(year + i);
			}
			for (var i = 1; i <= 12; i++) {
				_self.months.push(i);
			}
			/*获取所有年月，初始化已添加年月*/
			_self._getAllDates();
		},
		/*像服务器添加一条新的数据,并更新列表*/
		addMonth: function() {
			var _self = this;
			_self.selected.year.toString();
			var selected = JSON.stringify(_self.selected);
			selected = {
				"helpYear": _self.selected.year.toString(),
				"helpMonth": _self.selected.month
			}
			console.log(selected);
			$.ajax({
				type: "POST",
				url: _self.url.addMonth,
				data: JSON.stringify(selected),
				contentType: "application/json; charset=utf-8",
				dataType: 'json',
				success: function(result) {
					console.log(result);
					setTimeout(_self._getAllDates, 1000);
				}
			});

		},
		/*删除月份*/
		delete: function(el, id) {
			var _self = this;
			console.log(id);
			id=id.toString();
			var newUrl= _self.url.delMonth + '?' + 'id='+id;
			console.log(newUrl);
			$.ajax({
				type: "POST",
				url: newUrl,
				data: id,
				contentType: "application/json; charset=utf-8",
				dataType: 'json',
				success: function(result) {
					alert(result.message);
					_self._getAllDates();
				}
			})
		},
		/*设置金额*/
		setMoney: function() {
			var _self = this;
			// var je = JSON.stringify(_self.je);
			var je = {
				"je": parseInt(_self.je)
			}
			console.log(je);
			$.ajax({
				type: "POST",
				url: _self.url.modifyMoney,
				data: JSON.stringify(je),
				contentType: "application/json; charset=utf-8",
				dataType: 'json',
				success: function(result) {
					console.log(result);
					alert(result.message)
				}
			})
		},
		/*请求服务器，获取所有年月*/
		_getAllDates: function() {
			var _self = this;
			_self.dates = [];
			$.ajax({
				type: "GET",
				url: _self.url.getMonths,
				dataType: "json",
				success: function(result) {
					console.log(result);
					$.each(result.body, function(i, value) {
						_self.dates.push(value);
					})
				}
			})
		}
	}
})
month.init();